//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DiscretizedSwap.h"
using namespace Cephei::QL::Pricingengines::Swap;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Lattice.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/DiscretizedAsset.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::CDiscretizedSwap (boost::shared_ptr<QuantLib::DiscretizedSwap>& childNative, Object^ owner) : CDiscretizedAsset(CDiscretizedSwap::typeid)
{
#ifdef HANDLE
	_phDiscretizedSwap = NULL;
#endif
	_ppDiscretizedSwap = &childNative;
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedSwap));
}
Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::CDiscretizedSwap (QuantLib::DiscretizedSwap& childNative, Object^ owner) : CDiscretizedAsset(CDiscretizedSwap::typeid)
{
#ifdef HANDLE
	_phDiscretizedSwap = NULL;
#endif
	_ppDiscretizedSwap = new boost::shared_ptr<QuantLib::DiscretizedSwap> (&childNative);
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedSwap));
    _DiscretizedSwapOwner = owner;
    _DiscretizedAssetOwner = owner;
}

Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::CDiscretizedSwap (CDiscretizedSwap^ copy) : CDiscretizedAsset(CDiscretizedSwap::typeid)
{
#ifdef HANDLE
	_phDiscretizedSwap = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDiscretizedSwap = new boost::shared_ptr<QuantLib::DiscretizedSwap> (copy->GetShared());
        _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedSwap));
    }
}
Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::CDiscretizedSwap (System::Type^ t) : CDiscretizedAsset(CDiscretizedSwap::typeid)
{
#ifdef HANDLE
	_phDiscretizedSwap = NULL;
#endif
	if (!t->IsSubclassOf(CDiscretizedSwap::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::CDiscretizedSwap (QuantLib::Handle<QuantLib::DiscretizedSwap>& childNative, Object^ owner)  : CDiscretizedAsset(CDiscretizedSwap::typeid)
{
	_phDiscretizedSwap = &childNative;
	_ppDiscretizedSwap = &static_cast<boost::shared_ptr<QuantLib::DiscretizedSwap>>(childNative.currentLink());
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedSwap));
    _DiscretizedSwapOwner = owner;
}
Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::CDiscretizedSwap (QuantLib::Handle<QuantLib::DiscretizedSwap> childNative)  : CDiscretizedAsset(CDiscretizedSwap::typeid)
{
	_phDiscretizedSwap = &childNative;
	_ppDiscretizedSwap = &static_cast<boost::shared_ptr<QuantLib::DiscretizedSwap>>(childNative.currentLink());
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedSwap));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::CDiscretizedSwap (QuantLib::DiscretizedSwap childNative)  : CDiscretizedAsset(CDiscretizedSwap::typeid)
{
#ifdef HANDLE
	_phDiscretizedSwap = NULL;
#endif
	_ppDiscretizedSwap = new boost::shared_ptr<QuantLib::DiscretizedSwap> (new QuantLib::DiscretizedSwap (childNative));
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedSwap));
}
#endif

Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::~CDiscretizedSwap ()
{
    if (_ppDiscretizedSwap != NULL)
    {
	    delete _ppDiscretizedSwap;
        _ppDiscretizedSwap = NULL;
    }
}
Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::!CDiscretizedSwap ()
{
    if (_ppDiscretizedSwap != NULL)
    {
	    delete _ppDiscretizedSwap;
    }
}
QuantLib::DiscretizedSwap& Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::GetReference ()
{
    if (_ppDiscretizedSwap == NULL) throw gcnew NativeNullException ();
	return **_ppDiscretizedSwap;
}
boost::shared_ptr<QuantLib::DiscretizedSwap>& Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::GetShared ()
{
    if (_ppDiscretizedSwap == NULL) throw gcnew NativeNullException ();
	return *_ppDiscretizedSwap;
}
QuantLib::DiscretizedSwap* Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::GetPointer ()
{
    if (_ppDiscretizedSwap == NULL) throw gcnew NativeNullException ();
	return &**_ppDiscretizedSwap;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DiscretizedSwap>& Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::GetHandle ()
{
	if (_phDiscretizedSwap == NULL)
	{
		_phDiscretizedSwap = new Handle<QuantLib::DiscretizedSwap> (*_ppDiscretizedSwap);
	}
	return *_phDiscretizedSwap;
}
#endif
bool Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::HasNative () 
{
	return (_ppDiscretizedSwap != NULL);
}

Cephei::IVector<Double>^ Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::MandatoryTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppDiscretizedSwap)->mandatoryTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Pricingengines::Swap::IDiscretizedSwap^ Cephei::QL::Pricingengines::Swap::CDiscretizedSwap::Reset (UInt64 size)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size);
    	(*_ppDiscretizedSwap)->reset ( _size );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

